`
the user who owns the file (in this case, us). Lastly, we run the script
using dot slash notation (./) followed by the script’s name. The dot
(.) represents the current directory, so we’re essentially telling bash
to run helloworld.sh from the current working directory.
You can also run a bash script with the following syntax:
$ bash helloworld.sh
Because we specified the bash command, the script will run
using the bash interpreter and won’t require a shebang line. Also, if
you use the bash command, the script doesn’t have to be set with an
executable permission (+x). In later chapters, you’ll learn about the
permission model in more depth, and explore its importance in the
context of finding misconfigurations in penetration tests.
Debugging
Errors will inevitably occur when you’re developing bash scripts.
Luckily, debugging the script is quite intuitive. An easy way to
check for errors early is by running the script using the -n
parameter. This parameter will read the commands in the script but
won’t execute them, so if there are any syntax errors, they will be
shown on the screen. you can think of it as a dry-run method to test
validity of syntax:
bash -n script.sh
You can also use the -x parameter to turn on verbose mode,
which lets you see commands being executed and will help you
debug issues as the script executes in real time.
bash -x script.sh
If you want to start debugging at a given point in the script, you
can do this by including the set command in the script itself.
#!/bin/bash
set -x
--snip--
set +x
You can think of set as a valve that turns on and off a certain
option. The first command sets the debugging mode (set -x) while
the last command (set +x) disables it. Using it, you can avoid
Black Hat Bash (Early Access) © 2023 by Dolev Farhi and Nick Aleks